import { defineStore } from 'pinia';
import { menuTab } from 'stores/model';


export const useNavTabStore = defineStore('menu', {
  state: () => {
    return {
      routeTabs: [{ name: '主页', path: '/' }]
    }
  },

  getters: {

  },

  actions: {
    //增加路由Tab
    addRouteTabs (menu: menuTab): void {
      if (!this.routeTabs.find((r) => r.path === menu.path)) {
        this.routeTabs.push(menu);
      }
    },

    //删除路由Tab
    removeRouteTabs (menu: menuTab): void {
      let index = this.routeTabs.findIndex((r) => {
        return ((r.path === menu.path) && (r.name !== '主页'));
      });
      if (index > -1) {
        this.routeTabs.splice(index, 1);
      }
    },

    //得到下一个Tab
    getNextRouteTabs (menu: menuTab): menuTab {
      let index = this.routeTabs.findIndex((el) => {
        return (el.name === menu.name)
      });

      if ((index > -1) && (index < this.routeTabs.length - 1)) {
        return this.routeTabs[index + 1];
      } else if (index === this.routeTabs.length - 1) {
        return this.routeTabs[index - 1];
      }
      return {} as menuTab;
    }
  }
})
